package sockets;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class updatingChangesServer implements Runnable{


	
	
    @Override
	public void run() {
        ServerSocket serverSocket=null;
        PrintWriter out = null;
        BufferedReader in = null;
        try { 
         serverSocket = new ServerSocket(6668);
         System.out.println("Server started port 6668");
        }
        catch (IOException e) { 
         System.err.println("Cannot listen on port 6668");
         System.exit(-1);
        }
        Socket clientSocket=null;
        
		// TODO Auto-generated method stub
	    while(true){
		    try { 
		     System.out.println("updatingChangesServer - in the while ");
		     clientSocket=serverSocket.accept(); 
		     out=new PrintWriter(clientSocket.getOutputStream(),true);
		     in=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
		     // server code here...
		     //MyProtocol mp=new MyProtocol(in,out);
		     //mp.run();
		     InetAddress address=clientSocket.getInetAddress();
		     //run in different thread
		     changesServerProtocol chSerProt = new changesServerProtocol(in, out, address);
		     new Thread(chSerProt).start();
		     
		     System.out.println("finish");
		     //MyProtocol2 mp2 = new MyProtocol2();
		     //mp2.run();
		    }
		    catch (IOException e) { 
		     System.err.println("Accept failed");
		     System.exit(-1);
		    }
		    finally {
		     //if (clientSocket != null) clientSocket.close(); 
		     //if (serverSocket != null) serverSocket.close();
		    }    
	    }
	}
	

}
